package com.woniuxy.carlife.carlifeorderweb.controller;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.JSONObjectCodec;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.Result;
import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.StatusCode;
import com.woniuxy.carlife.carlifeorderweb.domain.AfterSell;
import com.woniuxy.carlife.carlifeorderweb.domain.ImgJsonObject;
import com.woniuxy.carlife.carlifeorderweb.param.AfterSellParam;
import com.woniuxy.carlife.carlifeorderweb.service.AfterSellService;
import com.woniuxy.carlife.carlifeorderweb.util.MinioTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
*@ClassName:AfterSellController
*@Description:关于售后相关接口
*@author qk
*@date 2021/4/7 14:25
**/
@Slf4j
@RestController
@RequestMapping("//after-sell")
public class AfterSellController {
    @Resource
    private MinioTemplate minioTemplate;
    @Autowired
    private AfterSellService afterSellService;
    /*
     * 功能描述〈根据门店id，返回所有客户的售后请求〉
     * @author:qk
     * @param:门店id
     * @return:包含AfterSell的Result
     * @Date 2021/4/7 14:26
     * @throws //
     */
    @GetMapping("/merchantmanage/afterSellOfService")
    public Result afterSellApply(int mid){
        List<AfterSell> afterSellBySid = afterSellService.getAfterSellBySid(mid);
        return new Result(true, StatusCode.OK,"查询成功",afterSellBySid);
    }
    /*
     * 功能描述〈查询购买平台商品的客户的所有的售后请求〉
     * @author:qk
     * @param:
     * @return:
     * @Date 2021/4/7 14:40
     * @throws //
     */
    @GetMapping("/platformmanage/afterSellOfProduct")
    public Result afterSellOfProduct(){
        return new Result();
    }
    /*
     * 功能描述〈用户提交售后请求〉
     * @author:qk
     * @param:
     * @return:
     * @Date 2021/4/8 15:29
     * @throws //
     */

    @PostMapping("/merchantmanage/addAfterSellOfService")
    public Result addAfterSellOfService(@ModelAttribute AfterSellParam afterSellParam){

        List<String> url=new ArrayList<>();
        for (MultipartFile causeImg :afterSellParam.getMultipartFile()){
            System.out.println(afterSellParam);
            //        图片上传
            LocalDate localDate=LocalDate.now();
            StringBuffer fileName=new StringBuffer("/");
            fileName.append(localDate.getYear()).append("/")
                    .append(localDate.getMonthValue()).append("/")
                    .append(localDate.getDayOfMonth()).append("/")
                    .append(System.currentTimeMillis());
            String type=causeImg.getOriginalFilename().substring(causeImg.getOriginalFilename().lastIndexOf("."));
            fileName.append(type);

            try {
                System.out.println(causeImg.getContentType());
                url.add(minioTemplate.uploadFile(fileName.toString(), "test", causeImg.getInputStream(), causeImg.getSize(), causeImg.getContentType()));
                log.info("上传图片成功");

            } catch (IOException e) {
                e.printStackTrace();
                log.error("上传图片失败");
                throw new RuntimeException("上传失败");
            }
        }
        ImgJsonObject imgJsonObject = new ImgJsonObject();
        imgJsonObject.setImgs(url);
        Object json = JSONObject.toJSON(imgJsonObject);
        String url1=json.toString();

//        上传原因等数据
        if(afterSellParam!=null){AfterSell afterSell = new AfterSell();
            afterSell.setOid(afterSellParam.getOid());
            afterSell.setCause(afterSellParam.getCause());
            afterSell.setWay(afterSellParam.getWay());
            afterSell.setImg(url1);
            QueryWrapper<AfterSell> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("oid",afterSellParam.getOid());
            AfterSell one = afterSellService.getOne(queryWrapper);
            if (one!=null){
                UpdateWrapper<AfterSell> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("oid",afterSellParam.getOid());
                afterSellService.update(afterSell, updateWrapper);
                log.info("售后申请修改成功");
                return new Result(true,StatusCode.OK,"售后申请修改成功");
            }else {
                afterSellService.save(afterSell);
                log.info("售后申请成功");
                return new Result(true,StatusCode.OK,"售后申请成功");
            }

        }
        return new Result(true,StatusCode.OK,"售后申请失败");
    }

}

